//
//  M1HomeDetailsHeaderView.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/16.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1HomeDetailsHeaderView.h"
#import "M1HomeDetailsHeaderCollectionViewCell.h"
#import "M1HomeModel.h"
#import "M1HomeDetailsCertificationVC.h"

@interface M1HomeDetailsHeaderView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *m1_collectionView;

@property (nonatomic, strong) UILabel *m1_page;

@property (nonatomic, strong) UILabel *m1_name;

@property (nonatomic, strong) UILabel *m1_location;

@property (nonatomic, strong) UILabel *m1_age;

@property (nonatomic, strong) UILabel *m1_tall;

@property (nonatomic, strong) UILabel *m1_address;

@property (nonatomic, strong) UIImageView *m1_vip;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) M1HomeModel *m1_model;

@property (nonatomic, strong) NSMutableArray *m1_listArr;

@end

@implementation M1HomeDetailsHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = m1_color(0x1f1f1f);
        self.m1_listArr = [NSMutableArray arrayWithCapacity:0];
        [self m1_createPhotos];
        [self m1_createDetails];
    }
    return self;
}

- (void)m1_likeAction:(UIButton *)sender{
    
}

- (void)m1_rightAction:(UIButton *)sender{
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    if ((tap.view.tag == 1 && [self.m1_model.basic.car_info.car_auth.auth_status integerValue] == 1) || (tap.view.tag == 2 && [self.m1_model.basic.house_info.house_auth.auth_status integerValue] == 1) || (tap.view.tag == 3 && [self.m1_model.basic.education_info.education_auth.auth_status integerValue] == 1) || (tap.view.tag == 4 && [self.m1_model.basic.identity_info.identity_auth.auth_status integerValue] == 1)) {
        M1HomeDetailsCertificationVC *homeVC = [[M1HomeDetailsCertificationVC alloc] init];
        homeVC.m1_changeBackImage = YES;
        homeVC.m1_changeNavTitleColor = YES;
        homeVC.m1_model = self.m1_model;
        homeVC.m1_title = @[@"汽车信息", @"房产信息", @"学历信息", @"身份信息"][tap.view.tag - 1];
        homeVC.m1_type = tap.view.tag - 1;
        homeVC.m1_changeNavTitleColor = YES;
        homeVC.m1_changeBackImage = YES;
        [self.m1_viewController.navigationController pushViewController:homeVC animated:YES];
    }
}

#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSInteger number = 1;

    if (self.m1_listArr.count > 0) {
        number = _m1_listArr.count;
    }
    
    return number;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREENWIDTH, SCREENWIDTH);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    M1HomeDetailsHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeDetailsCellIdentifier" forIndexPath:indexPath];
    
    if (self.m1_listArr.count == 0) {
        cell.m1_photoImage.image = LOAD_LOCAL_IMG(@"bgView");
    }else{
        [cell.m1_photoImage sd_setImageWithURL:[NSURL URLWithString:self.m1_listArr[indexPath.item]] placeholderImage:LOAD_LOCAL_IMG(@"bgView")];
    }
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat width = CGRectGetWidth(scrollView.frame);
    NSInteger currentPage = scrollView.contentOffset.x / width;
    
    self.m1_page.text = [NSString stringWithFormat:@"%ld/%ld", currentPage + 1, self.m1_listArr.count];
}
- (void)setM1_offset_y:(CGFloat)m1_offset_y{

    CGFloat y = m1_offset_y > SCREENWIDTH - STATUS_HEIGHT - 44 ? SCREENWIDTH - STATUS_HEIGHT - 44 : m1_offset_y;
    
    y = y < 0 ? 0 : y * (1.0 / (SCREENWIDTH - STATUS_HEIGHT - 44));
    
    y = 1 - y;
    
    self.m1_collectionView.alpha = y;
    
    self.m1_likeBtn.alpha = y;
}

- (void) m1_setModel:(M1HomeModel *)model{
    self.m1_model = model;
    
    if ([model.basic.avatar.img.url length] > 0) {
        [self.m1_listArr addObject:model.basic.avatar.img.url];
    }
    
    if (model.basic.photos.count > 0) {
        for (NSDictionary *url in model.basic.photos) {
            [self.m1_listArr addObject:url[@"img"][@"url"]];
        }
    }
        
    [self.m1_collectionView reloadData];
    
    self.m1_name.text = model.basic.name;

    if (model.status.member_fees_status == 1) {
        self.m1_vip.hidden = NO;
        [self.m1_vip mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.m1_name.mas_centerY);
            make.left.equalTo(self.m1_name.mas_right).offset(7);
            make.size.mas_equalTo(CGSizeMake(23, 12));
        }];
    }else{
        self.m1_vip.hidden = YES;
    }
   NSArray *address = [model.basic.abode componentsSeparatedByString:@"-"];
    if ([M1User user].status.member_fees_status == 1) {
       self.m1_location.text = [NSString stringWithFormat:@"%@，%@活跃",address.count>1?address[1]:address[0],[M1Founction m1_dateFormTimeStamp:model.status.visit_time]];
    }else{
        self.m1_location.text = address.count>1?address[1]:address[0];
    }
    
    [self.m1_likeBtn setTitle:model.status.liked_count forState:UIControlStateNormal];

    self.m1_page.text = [NSString stringWithFormat:@"1/%ld", self.m1_listArr.count];
    
    if (self.m1_listArr.count == 0) {
        self.m1_page.text = @"";
    }
    
    self.m1_age.text = [NSString stringWithFormat:@"%@岁", model.basic.age];

    [self.m1_age sizeToFit];
    
    [_m1_age mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.equalTo(self.m1_location.mas_bottom).offset(14);
        make.size.mas_equalTo(CGSizeMake(self.m1_age.frame.size.width + 20, 28));
    }];
    
    self.m1_tall.text = [NSString stringWithFormat:@"%@cm", model.basic.high];
    
    [self.m1_tall sizeToFit];
    
    [_m1_tall mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.m1_age.mas_right).offset(5);
        make.centerY.equalTo(self.m1_age.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(self.m1_tall.frame.size.width + 20, 28));
    }];
    
    NSArray *abodeArr = [model.basic.abode componentsSeparatedByString:@"-"];
    
    NSString *abode;
    if ([abodeArr count] > 0) {
        if ([[abodeArr firstObject] isEqualToString:@"北京"] || [[abodeArr firstObject] isEqualToString:@"上海"] || [[abodeArr firstObject] isEqualToString:@"天津"] || [[abodeArr firstObject] isEqualToString:@"重庆"]) {
            abode = [abodeArr firstObject];
        }else{
            abode = [abodeArr lastObject];
        }
    }else{
        abode = model.basic.abode;
    }
    
    self.m1_address.text = abode;
    
    [self.m1_address sizeToFit];
    
    [_m1_address mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.m1_tall.mas_right).offset(5);
        make.centerY.equalTo(self.m1_age.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(self.m1_address.frame.size.width + 20, 28));
    }];
    
    NSMutableArray *certificationArr = [NSMutableArray arrayWithCapacity:0];
    
    if ([model.basic.car_info.car_auth.auth_status integerValue] == 1) {
        [certificationArr addObject:@"personal_icon_cars"];
    }else{
        [certificationArr addObject:@"personal_icon_cars_no"];
    }
    
    if ([model.basic.house_info.house_auth.auth_status integerValue] == 1) {
        [certificationArr addObject:@"personal_icon_house"];
    }else{
        [certificationArr addObject:@"personal_icon_house_no"];
    }
    
    if ([model.basic.education_info.education_auth.auth_status integerValue] == 1) {
        [certificationArr addObject:@"personal_icon_education"];
    }else{
        [certificationArr addObject:@"personal_icon_education_no"];
    }
    
    if ([model.basic.identity_info.identity_auth.auth_status integerValue] == 1) {
        [certificationArr addObject:@"personal_icon_idcard"];
    }else{
        [certificationArr addObject:@"personal_icon_idcard_no"];
    }
    
    float with = 35;
    if (iPhone4) {
        with = 30;
    }
    for (int i = 0; i < certificationArr.count; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH - 15-with - with * i - 5 * i,iPhone4?24.5:27, with, with)];
        image.image = LOAD_LOCAL_IMG(certificationArr[i]);
        image.tag = i + 1;
        image.userInteractionEnabled = YES;
        [_bgView addSubview:image];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [image addGestureRecognizer:tap];
    }
}

- (void) m1_createPhotos{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    
    self.m1_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENWIDTH) collectionViewLayout:layout];
    _m1_collectionView.backgroundColor = m1_color(0xFFFFFF);
    _m1_collectionView.delegate = self;
    _m1_collectionView.dataSource = self;
    _m1_collectionView.showsVerticalScrollIndicator = NO;
    _m1_collectionView.showsHorizontalScrollIndicator = NO;
    _m1_collectionView.pagingEnabled = YES;
    
    [self addSubview:_m1_collectionView];
    
    [_m1_collectionView registerClass:[M1HomeDetailsHeaderCollectionViewCell class] forCellWithReuseIdentifier:@"homeDetailsCellIdentifier"];
    
    self.m1_likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _m1_likeBtn.backgroundColor = [m1_color(0x000000) colorWithAlphaComponent:0.2];
    [_m1_likeBtn setImage:LOAD_LOCAL_IMG(@"details_icon_heartbeat") forState:UIControlStateNormal];
    [_m1_likeBtn setTitle:@"0" forState:UIControlStateNormal];
    [_m1_likeBtn setTitleColor:m1_color(0xFFFFFF) forState:UIControlStateNormal];
    _m1_likeBtn.titleLabel.font = m1_font(13);
    [_m1_likeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
    [_m1_likeBtn addTarget:self action:@selector(m1_likeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_m1_likeBtn];
    
    _m1_likeBtn.layer.cornerRadius = 22 / 2;
    _m1_likeBtn.layer.masksToBounds = YES;
    
    [_m1_likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.bottom.offset(-140);
        make.size.mas_equalTo(CGSizeMake(50, 22));
    }];
    
    self.m1_page = [[UILabel alloc] init];
    _m1_page.textColor = m1_color(0xFFFFFF);
    _m1_page.font = m1_font(15);
    [self addSubview:_m1_page];

    [_m1_page mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m1_likeBtn.mas_centerY);
        make.centerX.equalTo(self.mas_centerX);
    }];
}

- (void) m1_createDetails{
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.m1_collectionView.frame), SCREENWIDTH, 130)];
    _bgView.backgroundColor = m1_color(0xFFFFFF);
    [self addSubview:_bgView];
    
    self.m1_name = [[UILabel alloc] init];
    _m1_name.text = @"nickname";
    _m1_name.textColor = m1_color(0x000000);
    _m1_name.font = m1_font(18);
    [_bgView addSubview:_m1_name];
    
    [_m1_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(21);
        make.top.offset(25);
        make.right.mas_lessThanOrEqualTo(-200);
    }];
    
    self.m1_vip = [[UIImageView alloc] init];
    _m1_vip.image = LOAD_LOCAL_IMG(@"home_icon_vip");
    [_bgView addSubview:_m1_vip];
    _m1_vip.hidden = YES;
    
    [_m1_vip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m1_name.mas_centerY);
        make.left.equalTo(self.m1_name.mas_right).offset(7);
        make.size.mas_equalTo(CGSizeMake(23, 12));
    }];
    
    self.m1_location = [[UILabel alloc] init];
    _m1_location.text = @"";
    _m1_location.textColor = m1_color(0x999999);
    _m1_location.font = m1_font(12);
    [_bgView addSubview:_m1_location];
    
    [_m1_location mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.equalTo(self.m1_name.mas_bottom).offset(11);
    }];
    
    self.m1_age = [[UILabel alloc] init];
    _m1_age.backgroundColor = m1_color(0xFCF0F0);
    _m1_age.text = @"0";
    _m1_age.textColor = m1_color(0xFF666B);
    _m1_age.textAlignment = NSTextAlignmentCenter;
    _m1_age.font = m1_font(12);
    [_bgView addSubview:_m1_age];
    
    _m1_age.layer.cornerRadius = 14.0;
    _m1_age.layer.masksToBounds = YES;
    
    [self.m1_age sizeToFit];
    
    [_m1_age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.equalTo(self.m1_location.mas_bottom).offset(14);
        make.size.mas_equalTo(CGSizeMake(self.m1_age.frame.size.width + 20, 28));
    }];
    
    self.m1_tall = [[UILabel alloc] init];
    _m1_tall.backgroundColor = m1_color(0xFCF0F0);
    _m1_tall.text = @"0";
    _m1_tall.textColor = m1_color(0xFF666B);
    _m1_tall.textAlignment = NSTextAlignmentCenter;
    _m1_tall.font = m1_font(12);
    [_bgView addSubview:_m1_tall];
    
    _m1_tall.layer.cornerRadius = 14.0;
    _m1_tall.layer.masksToBounds = YES;
    
    [self.m1_tall sizeToFit];
    
    [_m1_tall mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.m1_age.mas_right).offset(5);
        make.centerY.equalTo(self.m1_age.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(self.m1_tall.frame.size.width + 20, 28));
    }];
    
    self.m1_address = [[UILabel alloc] init];
    _m1_address.backgroundColor = m1_color(0xFCF0F0);
    _m1_address.text = @"所在地";
    _m1_address.textColor = m1_color(0xFF666B);
    _m1_address.textAlignment = NSTextAlignmentCenter;
    _m1_address.font = m1_font(12);
    [_bgView addSubview:_m1_address];
    
    _m1_address.layer.cornerRadius = 14.0;
    _m1_address.layer.masksToBounds = YES;
    
    [self.m1_address sizeToFit];
    
    [_m1_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.m1_tall.mas_right).offset(5);
        make.centerY.equalTo(self.m1_age.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(self.m1_address.frame.size.width + 20, 28));
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = m1_color(0xCCCCCC);
    [_bgView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(0.5);
    }];
    
}

@end
